<?php
declare (strict_types=1);

namespace app\api\controller;

use app\api\BaseController;
use app\api\service\ReportService;
use app\api\validate\DashbordValiate;
use app\api\validate\DeviceSyncValidate;
use app\traits\UserTrait;
use think\helper\Arr;
use think\response\Json;

class Report extends BaseController
{
    use UserTrait;

    private ReportService $service;

    public function initialize()
    {
        $this->service = new ReportService();
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 设备信息同步推送
     * @return Json
     * @throws \Exception
     */
    public function device(): Json
    {
        if(!$post = $this->request->all()) {
            return $this->error('推送数据不能为空');
        }
        foreach ($post as $item) {
            validate(DeviceSyncValidate::class)->check($item);
        }
        $this->service->deviceSync($post);
        return $this->success();
    }

    /**
     * 首页面板数据
     * @return Json
     */
    public function dashboard(): Json
    {
        if(!$post = $this->request->all()) {
            return $this->error('推送数据不能为空');
        }
        if (!$pushTime = Arr::get($post, 'push_time')) {
            return $this->error('缺少参数push_time');
        }
        if (!$items = Arr::get($post, 'items')) {
            return $this->error('缺少参数items');
        }
        foreach ($items as $item) {
            validate(DashbordValiate::class)->check($item);
        }
        $this->service->addDashboard($items, $pushTime);
        return $this->success();
    }

    /**
     * 粮情数据
     * @return Json
     */
    public function grainSituation(): Json
    {
        if(!$post = $this->request->all()) {
            return $this->error('推送数据不能为空');
        }
        if (!Arr::get($post, 'push_time')) {
            return $this->error('缺少参数push_time');
        }
        if (!Arr::get($post, 'grain_json')) {
            return $this->error('缺少参数grain_json');
        }
        if (!Arr::get($post, 'stock_id')) {
            return $this->error('缺少参数stock_id');
        }
        $this->service->addGrainSituation($post);
        return $this->success();
    }

    public function statistics()
    {
        
    }
}
